﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Tools.Model
{
    public class Proc
    {
        public string databasename { get; set; }
        public string name { get; set; }
        public string type { get; set; }
        public string definition { get; set; }

        public string GetProcDefintion()
        {
            var definitionClear = definition.Replace("[", "").Replace("]", "").Replace("\"", "").Replace("dbo.", "");

            ReplaceType(ref  definitionClear);

            var procDefinition = string.Format(@"if exists (select * from dbo.sysobjects where name = '{0}')
 drop procedure {0}
 GO
                                ", name);


            return procDefinition + definitionClear;
        }


        private void ReplaceType(ref string definition)
        {

            Dictionary<string, string> dicReplace = new Dictionary<string, string>();

            dicReplace["~STR_Length"] = "datalength";
            dicReplace["~T_TC_Quantity"] = "NUMERIC(25,6) DEFAULT(0)";
            dicReplace["~T_TC_Price"] = "NUMERIC(25,6) DEFAULT(0)";
            dicReplace["~T_TC_Amount"] = "NUMERIC(25,6) DEFAULT(0)";
            dicReplace["~T_TC_Rate"] = "NUMERIC(25,6) DEFAULT(0)";
            dicReplace["~T_TC_CodeText"] = "NVARCHAR(30)";
            dicReplace["~T_TC_Text"] = "NVARCHAR(255)";
            dicReplace["~T_TC_RulerText"] = "NVARCHAR(255) DEFAULT('')";
            dicReplace["~T_TC_ShortText"] = "NVARCHAR(80)";
            dicReplace["~T_TC_LongText"] = "NVARCHAR(2000)";
            dicReplace["~T_TC_ObjectID"] = "NVARCHAR(60)";
            dicReplace["~T_TC_ObjectSID"] = "NVARCHAR(10)";
            dicReplace["~T_TC_CharFlag"] = "NVARCHAR(1)";
            dicReplace["~T_TC_TallyStatus"] = "INTEGER";
            dicReplace["~T_TC_TallyVersion"] = "INTEGER DEFAULT(0)";
            dicReplace["~T_TC_YYYYMMDD"] = "NUMERIC(8)";
            dicReplace["~T_TC_IntFlag"] = "INTEGER";
            dicReplace["~T_TC_Integer"] = "INTEGER";
            dicReplace["~T_TC_Qualifier"] = "NVARCHAR(30)";
            dicReplace["~T_TC_Clock"] = "DATETIME";

            dicReplace["~T_TC_Quantity"] = "NUMERIC(25,6)";
            dicReplace["~T_TC_Price"] = "NUMERIC(25,6)";
            dicReplace["~T_TC_Amount"] = "NUMERIC(25,6)";
            dicReplace["~T_TC_Rate"] = "NUMERIC(25,6)";
            dicReplace["~T_TC_CodeText"] = "NVARCHAR(30)";
            dicReplace["~T_TC_Text"] = "NVARCHAR(255)";
            dicReplace["~T_TC_RulerText"] = "NVARCHAR(255)";
            dicReplace["~T_TC_ShortText"] = "NVARCHAR(80)";
            dicReplace["~T_TC_LongText"] = "NVARCHAR(2000)";
            dicReplace["~T_TC_ObjectID"] = "NVARCHAR(60)";
            dicReplace["~T_TC_ObjectSID"] = "NVARCHAR(10)";
            dicReplace["~T_TC_CharFlag"] = "NVARCHAR(1)";
            dicReplace["~T_TC_TallyStatus"] = "INTEGER";
            dicReplace["~T_TC_TallyVersion"] = "INTEGER";
            dicReplace["~T_TC_YYYYMMDD"] = "NUMERIC(8)";
            dicReplace["~T_TC_YYYYMMDD"] = "NUMERIC(8,0)";
            dicReplace["~T_TC_IntFlag"] = "INTEGER";
            dicReplace["~T_TC_Integer"] = "INTEGER";
            dicReplace["~T_TC_Qualifier"] = "NVARCHAR(20)";
            dicReplace["~T_TC_Clock"] = "DATETIME";
            foreach (var item in dicReplace)
            {
                definition = definition.Replace(item.Value.ToUpper(), item.Key);
                definition = definition.Replace(item.Value.ToLower(), item.Key);
                definition = System.Text.RegularExpressions.Regex.Replace(definition, item.Value, item.Key, System.Text.RegularExpressions.RegexOptions.IgnoreCase);

            }



        }

    }
}
